#!/bin/bash

# Ubuntu桌面环境 - Linux停止脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_message() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_header() {
    echo -e "${BLUE}================================${NC}"
    echo -e "${BLUE}  Ubuntu桌面环境停止脚本${NC}"
    echo -e "${BLUE}================================${NC}"
}

# 检查Docker服务
check_docker() {
    if ! command -v docker &> /dev/null; then
        print_error "Docker未安装"
        exit 1
    fi
    
    if ! docker info &> /dev/null; then
        print_error "Docker服务未运行"
        exit 1
    fi
}

# 显示运行中的容器
show_running_containers() {
    print_message "检查运行中的Ubuntu桌面环境容器..."
    
    local containers=()
    
    # 检查轻量级版本
    if docker ps --format "table {{.Names}}\t{{.Status}}" | grep -q "ubuntu-desktop-lightweight"; then
        containers+=("lightweight")
    fi
    
    # 检查标准版本
    if docker ps --format "table {{.Names}}\t{{.Status}}" | grep -q "ubuntu-desktop-xfce"; then
        containers+=("standard")
    fi
    
    if [ ${#containers[@]} -eq 0 ]; then
        print_message "没有运行中的Ubuntu桌面环境容器"
        return 1
    fi
    
    echo "发现运行中的容器："
    for container in "${containers[@]}"; do
        echo "  - $container"
    done
    
    return 0
}

# 停止指定版本的容器
stop_container() {
    local version=$1
    
    case $version in
        "lightweight")
            print_message "停止轻量级版本容器..."
            docker-compose -f docker-compose.lightweight.yml down
            ;;
        "standard")
            print_message "停止标准版本容器..."
            docker-compose down
            ;;
        *)
            print_error "未知版本: $version"
            return 1
            ;;
    esac
    
    print_message "$version 版本已停止"
}

# 停止所有版本
stop_all() {
    print_message "停止所有Ubuntu桌面环境容器..."
    
    # 停止轻量级版本
    if [ -f "docker-compose.lightweight.yml" ]; then
        docker-compose -f docker-compose.lightweight.yml down 2>/dev/null || true
    fi
    
    # 停止标准版本
    if [ -f "docker-compose.yml" ]; then
        docker-compose down 2>/dev/null || true
    fi
    
    print_message "所有容器已停止"
}

# 清理资源
cleanup_resources() {
    print_message "清理相关资源..."
    
    # 清理未使用的网络
    docker network prune -f
    
    # 清理未使用的卷
    docker volume prune -f
    
    print_message "资源清理完成"
}

# 显示状态
show_status() {
    print_message "当前状态："
    
    echo
    echo "容器状态："
    docker ps -a --filter "name=ubuntu-desktop" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
    
    echo
    echo "镜像状态："
    docker images --filter "reference=ubuntu-desktop" --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
    
    echo
    echo "网络状态："
    docker network ls --filter "name=desktop" --format "table {{.Name}}\t{{.Driver}}\t{{.Scope}}"
}

# 主函数
main() {
    print_header
    
    # 检查Docker
    check_docker
    
    # 显示运行中的容器
    if ! show_running_containers; then
        exit 0
    fi
    
    echo
    echo "请选择操作："
    echo "1) 停止轻量级版本"
    echo "2) 停止标准版本"
    echo "3) 停止所有版本"
    echo "4) 显示状态"
    echo "5) 清理资源"
    echo "6) 退出"
    echo
    
    read -p "请输入选择 (1-6): " choice
    
    case $choice in
        1)
            stop_container "lightweight"
            ;;
        2)
            stop_container "standard"
            ;;
        3)
            stop_all
            ;;
        4)
            show_status
            ;;
        5)
            cleanup_resources
            ;;
        6)
            print_message "退出"
            exit 0
            ;;
        *)
            print_error "无效选择"
            exit 1
            ;;
    esac
    
    echo
    print_message "操作完成"
}

# 捕获信号
trap 'print_error "脚本被中断"; exit 1' INT TERM

# 运行主函数
main "$@" 